package com.youaishujuhui.devops.web.dao.game;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;
import com.youaishujuhui.devops.web.domain.game.Game;

@Service
public class GameDao {
	@Resource(name = "nutDao")
    private Dao nutDao;

	public List<Game> query(Map<String, Object> param) {
		Cnd cnd = Cnd.where("state", "=", 1);
		
//		if (false == StringUtils.isNullOrEmpty((String) param.get("searchName"))) {
//			cnd = Cnd.where("name", "like", "%" + param.get("searchName") + "%");
//		}

		if (false == StringUtils.isNullOrEmpty((String) param.get("sort"))) {
			String sort = (String) param.get("sort");
			
			if (false == StringUtils.isNullOrEmpty((String) param.get("sort"))) {
				String order = (String) param.get("order");
				
				if (order.equals("asc")) {
					//orderBy = Cnd.orderBy().asc(sort);
					
					cnd.asc(sort);
				} else {
					//orderBy = Cnd.orderBy().desc(sort);
					
					cnd.desc(sort);
				}
			} else {
				//orderBy = Cnd.orderBy().asc(sort);
				
				cnd.asc(sort);
			}
		}
		
		int offset = (int) param.get("offset");
		int limit = (int) param.get("limit");
		Pager pager = new Pager(offset / limit + 1, limit);

		List<Game> games = nutDao.query(Game.class, cnd, pager);
		if (games != null) {
			for (Game game : games) {
				nutDao.fetchLinks(game, "department");
			}
		}
		
		return games;
	}

}
